const db = require('../../lib/conn_oracle')

exports.getLbList = async (req,res)=>{
    // console.log(req.body)
    let { form } = req.body
    let {lb_type,lb_sex,lb_height,lb_bust,lb_size,lb_stereotype} = form
    // console.log(lb_type)
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let reqDatas = []

    let lbTypeWhere = ""
    let lbSexWhere = ""
    let lbHeightWhere = ""
    let lbBustWhere = ""
    let lbSizeWhere = ""
    let lbStereotypeWhere = ""

    if(lb_type.length === 1){
        lbTypeWhere = `and lb_type = :lb_type`
        reqDatas.push(...lb_type)
    }else if(lb_type.length > 1){
        lbTypeWhere = `and lb_type in (`
        for (let i = 0; i < lb_type.length; i++)
        lbTypeWhere += (i > 0) ? ", :" + i : ":" + i
        lbTypeWhere += ")"
        reqDatas.push(...lb_type)
    }else{
        lbTypeWhere = ""
    }
    if(lb_sex.length === 1){
        lbSexWhere = `and lb_sex = :lb_sex`
        reqDatas.push(...lb_sex)
    }else if(lb_sex.length > 1){
        lbSexWhere = `and lb_sex in (`
        for (let i = 0; i < lb_sex.length; i++)
        lbSexWhere += (i > 0) ? ", :" + i : ":" + i
        lbSexWhere += ")"
        reqDatas.push(...lb_sex)
    }else{
        lbSexWhere = ""
    }
    if(lb_height.length === 1){
        lbHeightWhere = `and lb_height = :lb_height`
        reqDatas.push(...lb_height)
    }else if(lb_height.length > 1){
        lbHeightWhere = `and lb_height in (`
        for (let i = 0; i < lb_height.length; i++)
        lbHeightWhere += (i > 0) ? ", :" + i : ":" + i
        lbHeightWhere += ")"
        reqDatas.push(...lb_height)
    }else{
        lbHeightWhere = ""
    }
    if(lb_bust.length === 1){
        lbBustWhere = `and lb_bust = :lb_bust`
        reqDatas.push(...lb_bust)
    }else if(lb_bust.length > 1){
        lbBustWhere = `and lb_bust in (`
        for (let i = 0; i < lb_bust.length; i++)
        lbBustWhere += (i > 0) ? ", :" + i : ":" + i
        lbBustWhere += ")"
        reqDatas.push(...lb_bust)
    }else{
        lbBustWhere = ""
    }
    if(lb_size.length === 1){
        lbSizeWhere = `and lb_size = :lb_size`
        reqDatas.push(...lb_size)
    }else if(lb_size.length > 1){
        lbSizeWhere = `and lb_size in (`
        for (let i = 0; i < lb_size.length; i++)
        lbSizeWhere += (i > 0) ? ", :" + i : ":" + i
        lbSizeWhere += ")"
        reqDatas.push(...lb_size)
    }else{
        lbSizeWhere = ""
    }
    if(lb_stereotype.length === 1){
        lbStereotypeWhere = `and lb_stereotype = :lb_stereotype`
        reqDatas.push(...lb_stereotype)
    }else if(lb_stereotype.length > 1){
        lbStereotypeWhere = `and lb_stereotype in (`
        for (let i = 0; i < lb_stereotype.length; i++)
        lbStereotypeWhere += (i > 0) ? ", :" + i : ":" + i
        lbStereotypeWhere += ")"
        reqDatas.push(...lb_stereotype)
    }else{
        lbStereotypeWhere = ""
    }

    let sql = `select * from c##wxw.lb_list
               where 1 = 1
                 ${lbTypeWhere}
                 ${lbSexWhere}
                 ${lbHeightWhere}
                 ${lbBustWhere}
                 ${lbSizeWhere}
                 ${lbStereotypeWhere}   
               order by lb_type,lbnum  
              `
    let result = reqDatas.length ? await db.search(sql,reqDatas) : await db.search2(sql)
    resMsg = result.errorNum ? `获取失败` : `获取成功`
    resStatus = result.errorNum ? 0 : 1
    resDatas = { result }
    res.json({
        msg:resMsg,
        status: resStatus,
        resDatas
    })
}

exports.editLbList = async (req,res)=>{
    // console.log(req.body)
    let { type,form } = req.body
    let { LBNUM,DESCRIPTION,QUNIT,LB_TYPE,LB_SEX,LB_HEIGHT,LB_BUST,LB_SIZE,LB_STEREOTYPE } = form
    let reqData = []
    let msgType = ""
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let sql = ""
    if(type === 'add' || type === 'copy'){
        msgType = type === 'add' ? '添加' : '复制'
        sql = `insert into c##wxw.lb_list (LBNUM,DESCRIPTION,QUNIT,LB_TYPE,LB_SEX,LB_HEIGHT,LB_BUST,LB_SIZE,LB_STEREOTYPE)
               values (:LBNUM,:DESCRIPTION,:QUNIT,:LB_TYPE,:LB_SEX,:LB_HEIGHT,:LB_BUST,:LB_SIZE,:LB_STEREOTYPE)
              `
        reqData = [LBNUM,DESCRIPTION,QUNIT,LB_TYPE,LB_SEX,LB_HEIGHT,LB_BUST,LB_SIZE,LB_STEREOTYPE]
        let reqSql = `select * from c##wxw.lb_list where lbnum = :LBNUM`
        let lbListInfo = await db.search(reqSql,[LBNUM])
        if(lbListInfo.length){
            res.json({
                msg:`新增失败，LBNUM${LBNUM}已存在`,
                status:0
            })
            return
        }
    }
    if(type === 'update'){
        msgType = '更新'
        sql = `update c##wxw.lb_list
               set DESCRIPTION = :DESCRIPTION,
                   QUNIT = :QUNIT,
                   LB_TYPE = :LB_TYPE,
                   LB_SEX = :LB_SEX,
                   LB_HEIGHT = :LB_HEIGHT,
                   LB_BUST = :LB_BUST,
                   LB_SIZE = :LB_SIZE,
                   LB_STEREOTYPE = :LB_STEREOTYPE
               where LBNUM = :LBNUM
              `
        reqData = [DESCRIPTION,QUNIT,LB_TYPE,LB_SEX,LB_HEIGHT,LB_BUST,LB_SIZE,LB_STEREOTYPE,LBNUM]
    }
    if(type === 'delete'){
        msgType = '删除'
        sql = `delete from c##wxw.lb_list
               where LBNUM = :LBNUM
              `
        reqData = [LBNUM]
    }

    try{
        let result = await db.writeTab(sql,reqData)
        console.log(result)
        resMsg = result.errorNum ? `${msgType}失败,${result}` : `${msgType}成功`
        resStatus = result.errorNum ? 0 : 1
        resDatas = { result }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}

exports.editManyLbList = async (req,res)=>{     
    // console.log(req.body)
    let { type,form } = req.body
    let options = {
        autoCommit: true
    }
    let msgType = ""
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let sql = ""
    if(type === 'batchAdd'){
        msgType = '新增'
        sql = `insert into c##wxw.lb_list (LBNUM,DESCRIPTION,QUNIT,LB_TYPE,LB_SEX,LB_HEIGHT,LB_BUST,LB_SIZE,LB_STEREOTYPE)
               values (:LBNUM,:DESCRIPTION,:QUNIT,:LB_TYPE,:LB_SEX,:LB_HEIGHT,:LB_BUST,:LB_SIZE,:LB_STEREOTYPE)
              `
        let needAddLbs = []
        form.forEach(item => {
            item.LBNUM && needAddLbs.push(item.LBNUM)
        });
        // console.log(needAddPgs)
        let searchSql = `select p.lbnum
                         from c##wxw.lb_list p
                         where p.lbnum in ('${needAddLbs.join("','")}')
                        `
        // console.log(searchSql)
        let lbListInfo = await db.search2(searchSql)
        // console.log(lbListInfo)
        let lbnumList = lbListInfo.map(item => item.LBNUM)
        let diff = !lbnumList.length ? [] : needAddLbs.filter(item => lbnumList.includes(item))
        // console.log(diff)
        if(diff.length){
            res.json({
                msg:`${msgType}失败，存在重复的物资编码：${diff.join(",")}`,
                status:0
            })
            return
        }
    }
    if(type === 'batchUpdate'){
        msgType = '更新'
        sql = `update c##wxw.lb_list
               set DESCRIPTION = :DESCRIPTION,
                   QUNIT = :QUNIT,
                   LB_TYPE = :LB_TYPE,
                   LB_SEX = :LB_SEX,
                   LB_HEIGHT = :LB_HEIGHT,
                   LB_BUST = :LB_BUST,
                   LB_SIZE = :LB_SIZE,
                   LB_STEREOTYPE = :LB_STEREOTYPE
               where LBNUM = :LBNUM
              `
    }
    if(type === 'batchDel'){
        msgType = '删除'
        sql = `delete from c##wxw.lb_list
               where LBNUM = :LBNUM
              `
    }
    try{
        let result = await db.writeManyTab(sql,form,options)
        resMsg = result.errorNum ? `${msgType}失败,${result}` : `${msgType}成功`
        resStatus = result.errorNum ? 0 : 1
        resDatas = { result }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}
